package cn.rengy.web.framework.sys.pub.upload.web;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections4.MapUtils;
import org.apache.http.client.ClientProtocolException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.rengy.lang.ResultEntity;
import cn.rengy.tool.core.util._NumberUtils;
import cn.rengy.web.framework.sys.pub.upload.service.PubFileService;


/**
 * 
 * 
 */
@Controller
@RequestMapping
public class UploadController {
	private static Logger logger = LoggerFactory.getLogger(UploadController.class);
	@Autowired
	private PubFileService pubFileService;
	
	@RequestMapping(value="/upload", method = RequestMethod.POST)
	@ResponseBody
    public ResultEntity<?> upload1(HttpServletRequest request) throws IOException{
		//碎片文件没有id，所以返回可能为空，上传完成后不为空
		return ResultEntity.ok(this.pubFileService.upload(request));
    }
	/**
	 * 文件上传
	 * @param request
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value="/authenticated/upload", method = RequestMethod.POST)
	@ResponseBody
    public ResultEntity<?> upload(HttpServletRequest request) throws IOException{
		//碎片文件没有id，所以返回可能为空，上传完成后不为空
		return ResultEntity.ok(this.pubFileService.upload(request));
    }
	
	@RequestMapping(value="/authenticated/wxImage")
	@ResponseBody
    public ResultEntity<?> wxUploadImage(@RequestBody Map<String,Object> paramMap) throws ClientProtocolException, IOException{
		return ResultEntity.ok(this.pubFileService.wxTempMedia(paramMap));
    }
	
	/**
	 * 删除图片与目标关系
	 * @param paramMap
	 * @return
	 */
	@RequestMapping(value="/authenticated/deleterel", method = RequestMethod.POST)
	@ResponseBody
    public ResultEntity<?> deleterel(@RequestBody Map<String,Object> paramMap){
		this.pubFileService.deleterel(paramMap);
		return ResultEntity.ok();
    }
	/**
	 * 删除图片，只是更改删除状态
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/authenticated/delete", method = RequestMethod.POST)
	@ResponseBody
    public ResultEntity<?> delete(@RequestBody Map<String,Object> paramMap){
		this.pubFileService.delete(paramMap);
		return ResultEntity.ok();
    }
	/**
	 * 删除图片文件，真实的删除
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/authenticated/realDelete", method = RequestMethod.POST)
	@ResponseBody
    public ResultEntity<?> realDelete(@RequestBody Map<String,Object> paramMap){
		Long id=(Long)paramMap.get("ids");
		this.pubFileService.realDelete(paramMap);
		return ResultEntity.ok();
    }
	/**
	 * 富文本编辑器内浏览图片
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/authenticated/browse")
	@ResponseBody
    public Map<String,Object> browse(@RequestBody(required=false) Map<String,Object> paramMap){
		return this.pubFileService.browse(paramMap);
    }
	@RequestMapping(value="/authenticated/saveSingle")
	@ResponseBody
    public ResultEntity<?> saveSingle(@RequestBody Map<String,Object> paramMap){
			Long file_id=_NumberUtils.toLong(paramMap.get("file_id"), 0L);
			Long tartget_id=MapUtils.getLong(paramMap,"tartget_id");
			Long file_item_id=MapUtils.getLong(paramMap,"file_item_id");
			pubFileService.saveSingle(file_id, tartget_id, file_item_id);
		return ResultEntity.ok();
    }
}
